package com.pearadmin.edu.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.common.tools.servlet.ServletUtil;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.edu.domain.Course;
import com.pearadmin.edu.service.ICarouselService;
import com.pearadmin.edu.service.ICourseService;
import com.pearadmin.edu.service.IMenuService;
import com.pearadmin.system.domain.SysDictData;
import com.pearadmin.system.domain.SysUser;
import com.pearadmin.system.service.ISysDictDataService;
import com.pearadmin.system.service.ISysUserService;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.wf.captcha.utils.CaptchaUtil.*;

/**
 * 前台管理Controller
 *
 * @author yanyi
 * @date 2021-07-04
 */
@Controller
@RequestMapping("/front")
@Slf4j
public class FrontController extends BaseController {
    @Resource
    private ICourseService courseService;
    @Resource
    private ICarouselService carouselService;
    @Resource
    private IMenuService menuService;
    @Resource
    private ISysDictDataService dictDataService;
    @Resource
    private ISysUserService sysUserService;

    /**
     * 首页
     *
     * @param model
     * @return
     */
    @GetMapping({"/index", "/"})
    public String index(Model model) {
        SysUser user = (SysUser) ServletUtil.getSession().getAttribute("currentUser");
        Course Ggkc = new Course();
        Ggkc.setGgkc(1);
        Course Zykc = new Course();
        Zykc.setZykc(1);
        Course Mfkc = new Course();
        Mfkc.setMfkc(1);
        model.addAttribute("menu", menuService.selectMenuList(null));
        model.addAttribute("lunbo", carouselService.selectCarouselList(null));
        model.addAttribute("ggkc", courseService.selectCourseList(Ggkc));
        model.addAttribute("zykc", courseService.selectCourseList(Zykc));
        model.addAttribute("mfkc", courseService.selectCourseList(Mfkc));
        model.addAttribute("user", user);
        return "edu/front/index.html";
    }

    /**
     * 课程首页
     *
     * @param model
     * @return
     */
    @GetMapping("/course")
    public String course(Model model) {
        model.addAttribute("menu", menuService.selectMenuList(null));
        model.addAttribute("classify", dictDataService.selectByCode("sys_course_class"));
        return "edu/front/course";
    }

    /**
     * 分页查询
     *
     * @param course
     * @param pageDomain
     * @return
     */
    @ResponseBody
    @RequestMapping("/course/page")
    public ResultTable page(@ModelAttribute Course course, PageDomain pageDomain) {
        PageInfo<Course> pageInfo = courseService.selectCoursePage(course, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 课程详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/course/{id}")
    public String detail(@PathVariable("id") Integer id, Model model) {
        Course course = courseService.selectCourseById(id);
        SysUser teacher = sysUserService.getById(course.getTeacherId());
        SysDictData sysDictData = new SysDictData();
        sysDictData.setTypeCode("sys_course_class");
        sysDictData.setDataValue(course.getClassifyId().toString());
        List<SysDictData> dictData = dictDataService.list(sysDictData);
        if (dictData != null && dictData.size() > 0) {
            model.addAttribute("classify", dictData.get(0));
        }
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPage(1);
        pageDomain.setLimit(10);

        model.addAttribute("order", null);
        model.addAttribute("course", course);
        model.addAttribute("teacher", teacher);
        //model.addAttribute("videos", videoService.list());
        model.addAttribute("menu", menuService.selectMenuList(null));
        model.addAttribute("tuijian", courseService.selectCourseList(null));
        return "edu/front/detail.html";
    }

    @GetMapping("/login")
    public String login(Model model) {
        SysUser user = (SysUser) ServletUtil.getSession().getAttribute("currentUser");
        model.addAttribute("menu", menuService.selectMenuList(null));
        if (user != null) {
            model.addAttribute("user", user);
            return "edu/front/index.html";
        } else {
            return "edu/front/login.html";
        }
    }

    /**
     * 用户登录
     */
    //@ResponseBody
    //@PostMapping("/tologin")
    //public Result tologin(String username, String password, String code, Boolean remember, HttpServletRequest request) {
    //    try {
    //        if (StringUtils.isBlank(username)) {
    //            failure("请输入账号");
    //        }
    //        if (!CaptchaUtil.ver(code, request)) {
    //            return failure("验证码不正确");
    //        }
    //        if (remember == null) {
    //            remember = false;
    //        }
    //        return success();
    //        //SecurityUtils.getSubject().login(new CustomizedToken(username, password, remember, "User"));
    //    } catch (Exception e) {
    //        return failure(e.getMessage());
    //    }
    //}
    //@GetMapping("/login")
    //public String login(HttpServletRequest request){
    //    if (SecurityUtil.isAuthentication()) {
    //        return jumpPage("edu/front/index.html");
    //    }else{
    //        return jumpPage("edu/front/login.html");
    //    }
    //}


    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        try {
            CaptchaUtil.out(4, request, response);
        } catch (Exception e) {
            log.error("captcha:", e.getMessage());
        }
    }
}
